package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.GxUserDept;
import com.ruoyi.system.mapper.GxUserDeptMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GxDeptMapper;
import com.ruoyi.common.core.domain.entity.GxDept;
import com.ruoyi.system.service.IGxDeptService;
import com.ruoyi.system.domain.GxDeptAssess;
import com.ruoyi.system.mapper.GxDeptAssessMapper;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 部门Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-14
 */
@Service
public class GxDeptServiceImpl implements IGxDeptService
{
    @Autowired
    private GxDeptMapper gxDeptMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private GxDeptAssessMapper deptAssessMapper;

    @Autowired
    private GxUserDeptMapper userDeptMapper;

    /**
     * 查询部门
     *
     * @param deptId 部门主键
     * @return 部门
     */
    @Override
    public GxDept selectGxDeptByDeptId(Long deptId)
    {
        return gxDeptMapper.selectGxDeptByDeptId(deptId);
    }

    /**
     * 查询部门列表
     *
     * @param gxDept 部门
     * @return 部门
     */
    @Override
    public List<GxDept> selectGxDeptList(GxDept gxDept)
    {
        //获取系统用户
        LoginUser loginUser = getLoginUser();
        SysUser user = loginUser.getUser();
        Long userId = user.getUserId();
        //判断用户角色
        List<SysRole> roles = user.getRoles();
        if (user.isAdmin()) {
            return gxDeptMapper.selectGxDeptList(gxDept);
        }
        for (SysRole role : roles) {
            String roleName = role.getRoleName();
            if (roleName.equals("管理员")) {
                return gxDeptMapper.selectGxDeptList(gxDept);
            }
        }
        //用户团队列表
        GxUserDept userDept = new GxUserDept();
        userDept.setUserId(userId);
        //查询担任组长的团队列表
        userDept.setSenior('1');
        List<GxUserDept> uds = userDeptMapper.selectGxUserDeptList(userDept);
        //返回
        List<GxDept> depts = new ArrayList<>();
        for (GxUserDept ud: uds) {
            Long deptId = ud.getDeptId();
            GxDept d = gxDeptMapper.selectGxDeptByDeptId(deptId);
            depts.add(d);
        }
        return depts;

    }

    public void insertDeptAuthAssess(Long deptId, Long[] assessIds){
        deptAssessMapper.deleteDeptAssessByDeptId(deptId);
        insertDeptAssess(deptId, assessIds);

    }

    public void insertDeptAssess(Long deptId, Long[] assessIds){
        if (StringUtils.isNotNull(assessIds))
        {
            // 新增用户与评定管理
            List<GxDeptAssess> list = new ArrayList<GxDeptAssess>();
            for (Long assessId : assessIds)
            {
                GxDeptAssess da = new GxDeptAssess();
                da.setDeptId(deptId);
                da.setAssessId(assessId);
                list.add(da);
            }
            if (list.size() > 0)
            {
                deptAssessMapper.batchDeptAssess(list);
            }
        }
    }

    /**
     * 根据角色ID查询部门树信息
     *
     * @param roleId 角色ID
     * @return 选中部门列表
     */
    @Override
    public List<Long> selectDeptListByRoleId(Long roleId)
    {
        SysRole role = roleMapper.selectRoleById(roleId);
        return gxDeptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
    }

    /**
     * 新增部门
     *
     * @param gxDept 部门
     * @return 结果
     */
    @Override
    public int insertGxDept(GxDept gxDept)
    {
        gxDept.setCreateTime(DateUtils.getNowDate());
        return gxDeptMapper.insertGxDept(gxDept);
    }

    /**
     * 修改部门
     *
     * @param gxDept 部门
     * @return 结果
     */
    @Override
    public int updateGxDept(GxDept gxDept)
    {
        gxDept.setUpdateTime(DateUtils.getNowDate());
        return gxDeptMapper.updateGxDept(gxDept);
    }

    /**
     * 批量删除部门
     *
     * @param deptIds 需要删除的部门主键
     * @return 结果
     */
    @Override
    public int deleteGxDeptByDeptIds(Long[] deptIds)
    {
        return gxDeptMapper.deleteGxDeptByDeptIds(deptIds);
    }

    /**
     * 删除部门信息
     *
     * @param deptId 部门主键
     * @return 结果
     */
    @Override
    public int deleteGxDeptByDeptId(Long deptId)
    {
        deptAssessMapper.deleteDeptAssessByDeptId(deptId);
        return gxDeptMapper.deleteGxDeptByDeptId(deptId);
    }

    /**
     * 查询部门是否存在用户
     *
     * @param deptId 部门ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkDeptExistUser(Long deptId)
    {
        int result = gxDeptMapper.checkDeptExistUser(deptId);
        return result > 0;
    }



    /**
     * 校验部门是否有数据权限
     *
     * @param deptId 部门id
     */
    @Override
    public void checkDeptDataScope(Long deptId)
    {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            GxDept dept = new GxDept();
            dept.setDeptId(deptId);
            List<GxDept> depts = SpringUtils.getAopProxy(this).selectGxDeptList(dept);
            if (StringUtils.isEmpty(depts))
            {
                throw new ServiceException("没有权限访问部门数据！");
            }
        }
    }
}
